package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.constant.ResultConstants;
import com.bjpowernode.crm.commons.domain.DataResult;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Controller
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/settings/qx/user/toLogin.do")
    public String toLogin(){
        return "settings/qx/user/login";
    }


    @RequestMapping("/settings/qx/user/login.do")
    @ResponseBody
    public DataResult<User> login(String loginAct, String loginPwd , String isRemPwd,
                                  HttpServletRequest request, HttpSession session, HttpServletResponse response){

        DataResult dataResult = new DataResult<> ();

        Map<String,Object> map = new HashMap<>();
        map.put("loginAct", loginAct);
        map.put("loginPwd", loginPwd);

        User user = userService.queryUserByLoginActAndPwd(map);
        if (ObjectUtils.isEmpty(user)){
            //用户名或密码错误
            dataResult.setCode(ResultConstants.RETURN_CODE_FAIL);
            dataResult.setMsg("登录失败，用户名或密码错误");
        }else {
            //判断用户是否合法;
//            String newData = dateFormat.format(new Date());
            String date = DateUtils.formateDateTimeUtils(new Date());
            //compareTo比较大小，前面的大，返回1
            if (date.compareTo(user.getExpireTime()) > 0){
                dataResult.setCode(ResultConstants.RETURN_CODE_FAIL);
                dataResult.setMsg("登录失败，用户已过期");
            }else if (user.getLockState().equals("0")){
                dataResult.setCode(ResultConstants.RETURN_CODE_FAIL);
                dataResult.setMsg("登录失败，用户被锁定");
            //判断是否在常用地址中,获取远程IP ：request.getRemoteAddr()
            }else if (!user.getAllowIps().contains(request.getRemoteAddr())){
                dataResult.setCode(ResultConstants.RETURN_CODE_FAIL);
                dataResult.setMsg("登录失败，不在常用地址");
            }else{
                //登陆成功
                dataResult.setCode(ResultConstants.RETURN_CODE_SUCCESS);
                dataResult.setMsg("登录成功");

                //数据保存在session中
                session.setAttribute(ResultConstants.SESSION_USER, user);

                //用户选择保存密码，写入cookie,并响应给前端
                if ("true".equals(isRemPwd)){
                    Cookie co1 = new Cookie("loginAct", user.getLoginAct());
                    co1.setMaxAge(10 * 24 * 60 *60);
                    Cookie co2 = new Cookie("loginPwd", user.getLoginAct());
                    co2.setMaxAge(10 * 24 * 60 *60);
                    response.addCookie(co1);
                    response.addCookie(co2);
                }else {
                    //用户不保存密码，删除cookie；设置为0，即是删除
                    Cookie co1 = new Cookie("loginAct", "1");
                    co1.setMaxAge(0);
                    Cookie co2 = new Cookie("loginPwd", "1");
                    co2.setMaxAge(0);
                    response.addCookie(co1);
                    response.addCookie(co2);
                }
            }
        }

        return dataResult;
    }
}
